package org.enwey.icarenew.Manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class LoginManager {
    
	private static LoginManager instance;
	private static SharedPreferences preferences;
	private static final String KEY_SESSION = "Session";

    private String value;

	private LoginManager(Context context){
        
        preferences = context.getSharedPreferences("Session", Context.MODE_PRIVATE);
        
	}
	
	public static LoginManager getInstance(Context context) {
        synchronized (LoginManager.class){
            if (instance == null){
                synchronized (LoginManager.class){
                    instance = new LoginManager(context.getApplicationContext());
                }
            }
        }
		return instance;
	}

	public String getLoginSession() {
        if (TextUtils.isEmpty(value)){
            value = preferences.getString(KEY_SESSION, "");
        }
        return value;
	}

	public void deleteLoginSession() {
        value = null;
        preferences.edit().remove(KEY_SESSION).commit();
	}

	public void saveLoginSession(String sessionId) {
        if (TextUtils.isEmpty(sessionId)){
            return;
        }

        if (! sessionId.equals(value)){
            preferences.edit().putString(KEY_SESSION, sessionId).commit();
        }
	}

	public boolean isLogin() {
        String sessionStr = getLoginSession();
        return !TextUtils.isEmpty(sessionStr);
	}


}
